package com.sunnyatwal.kettlebell;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.sunnyatwal.kettlebell.model.ExerciseUnit;
import com.sunnyatwal.kettlebell.model.SubExercise;

public class EditExerciseActivity extends SherlockActivity {	
	
	private final static String TAG = "EditExerciseActivity";
	
	private final static String INTENT_NAME_EXERCISE = "exercise";
	private final static int INVALID_LIST_INDEX = -1;
	
	
	@Override 
	protected void onStart() {
		Log.i(TAG, "onStart");
		super.onStart();
	}
	
	@Override
	protected void onStop() {
    	Log.i(TAG, "onStop");
    	super.onStop();
    }
	
	
	@Override
	protected void onRestart() {
		Log.i(TAG, "onRestart");
		super.onRestart();
	}
	
	
	@Override
	protected void onResume() {
		Log.i(TAG, "onResume");
		super.onResume();		    			
	}
	
	
	@Override
	protected void onDestroy() {
		Log.i(TAG, "onDestroy");
		super.onDestroy();		
	}
	
	
	@Override
    public void onBackPressed() {
		Log.i(TAG, "onBackPressed");
		setResult(Activity.RESULT_CANCELED, new Intent());
		super.onBackPressed();
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		Log.i(TAG, "onCreate");
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_edit_exercise);
		
    	getActionBar().setDisplayHomeAsUpEnabled(false);	
		getActionBar().setDisplayShowTitleEnabled(false);
				
		//set up views						
		final Spinner spinnerWorkout = (Spinner) findViewById(R.id.edit_exercise_workout_spinner);
		ArrayAdapter<CharSequence> spinnerWorkoutAdapter = 
					ArrayAdapter.createFromResource(this, R.array.list_exercises, android.R.layout.simple_spinner_dropdown_item);
    	
		spinnerWorkout.setAdapter(spinnerWorkoutAdapter);
		
		final Spinner spinnerWeight = (Spinner) findViewById(R.id.edit_exercise_weight_spinner);
		ArrayAdapter<CharSequence> spinnerWeightAdapter = 
				ArrayAdapter.createFromResource(this, R.array.list_weights, android.R.layout.simple_spinner_dropdown_item);
    	
		spinnerWeight.setAdapter(spinnerWeightAdapter);
		
		final NumberPicker mins = (NumberPicker) findViewById(R.id.edit_exercise_time_mins);
		mins.setMaxValue(59);
        mins.setMinValue(0);   
    	mins.setWrapSelectorWheel(true);
		
		final NumberPicker secs = (NumberPicker) findViewById(R.id.edit_exercise_time_secs);
		secs.setMaxValue(59);
        secs.setMinValue(0);   
    	secs.setWrapSelectorWheel(true);
    	
		final NumberPicker reps = (NumberPicker) findViewById(R.id.edit_exercise_reps);
		reps.setMaxValue(100);
        reps.setMinValue(1);
    	        
        //get exercise
  		if (getIntent().hasExtra(INTENT_NAME_EXERCISE)) {
  			SubExercise mIntentExercise = getIntent().getParcelableExtra(INTENT_NAME_EXERCISE);
  			populateFields(mIntentExercise, spinnerWorkout, spinnerWorkoutAdapter, spinnerWeight, spinnerWeightAdapter, mins, secs, reps);
  		}                                      
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i(TAG, "onCreateOptionsMenu");
		getSupportMenuInflater().inflate(R.menu.activity_edit_exercise, menu);
		return true;
	}
	
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		Log.i(TAG, "onOptionsItemSelected");
		
        switch (item.getItemId()) {	 
        
	        case R.id.menu_edit_exercise_save:			
				sendResultToEditWorkoutActivity();				
			break;
			
	        case android.R.id.home:
	        case R.id.menu_edit_exercise_back:
	    		onBackPressed();
	    	break;	        
        }
        
        return super.onOptionsItemSelected(item);
    }
	
	 
	private void populateFields(SubExercise exercise, Spinner spinnerWorkout, ArrayAdapter<CharSequence> spinnerWorkoutAdapter, 
			Spinner spinnerWeight, ArrayAdapter<CharSequence> spinnerWeightAdapter, NumberPicker mins, NumberPicker secs, NumberPicker reps) {		
		
		Log.i(TAG, "populateFields");
		
		int weightIndex = spinnerWeightAdapter.getPosition(String.valueOf(exercise.getWeight()));				
		if (weightIndex > INVALID_LIST_INDEX) {
			spinnerWeight.setSelection(weightIndex);
		}
		else {
			Log.i(TAG, "Invalid weight index");
		}

		int workIndex = spinnerWorkoutAdapter.getPosition(exercise.getName());
		if (workIndex > INVALID_LIST_INDEX) {
			spinnerWorkout.setSelection(workIndex);
		}
		else {
			Log.i(TAG, "Invalid workout index");
		}
	    		
		if(exercise.getTotalTime() > 0) {
			mins.setValue(exercise.getTotalTimeMinutes());
			secs.setValue(exercise.getTotalTimeSeconds());
		}
		else {
			Log.i(TAG, "Invalid time");
		}
		
		reps.setValue((int)exercise.getReps()); 		
	}
	
	
	private void sendResultToEditWorkoutActivity() {		
		Log.i(TAG, "sendResultToEditWorkoutActivity");
		
		final Spinner spinnerWorkout = (Spinner) findViewById(R.id.edit_exercise_workout_spinner);
		final Spinner spinnerWeight = (Spinner) findViewById(R.id.edit_exercise_weight_spinner);
		
		final NumberPicker mins = (NumberPicker) findViewById(R.id.edit_exercise_time_mins);
		final NumberPicker secs = (NumberPicker) findViewById(R.id.edit_exercise_time_secs);
		final NumberPicker reps = (NumberPicker) findViewById(R.id.edit_exercise_reps);
		
		//convert numberpicker times to milliseconds
		int time = ExerciseUnit.convert(mins.getValue(), secs.getValue(), true);
		
		if(time > 0) {
			
			SubExercise intentExercise = new SubExercise(spinnerWorkout.getSelectedItem().toString(), 
					time, Integer.parseInt(spinnerWeight.getSelectedItem().toString()), reps.getValue());
			
			Intent intent = new Intent();
			intent.putExtra(INTENT_NAME_EXERCISE, intentExercise);
			
			setResult(Activity.RESULT_OK, intent);
			finish();			
		}
		else {
			
			Toast toast = Toast.makeText(getApplicationContext(), 
					getString(R.string.string_zero_time), Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 30);
			toast.show();			
		}
	}
	
	
}
